#include "inputsettingsdialog.h"
#include "ui_inputsettingsdialog.h"

#include <QKeyEvent>

#include "mainwindow.h"




InputSettingsDialog::InputSettingsDialog(void* mainWindow,QWidget* parent):QDialog(parent),ui(new Ui::InputSettingsDialog){
  ui->setupUi(this);

  this->mainWindow=mainWindow;
  IsInit=false;

  Buttons_Init(0,ui->pushButton_1P_A,H_NES_CPP::StandardControllerStatus::Pad1P_A);
  Buttons_Init(1,ui->pushButton_1P_AutoA,H_NES_CPP::StandardControllerStatus::Pad1P_AutoA);
  Buttons_Init(2,ui->pushButton_1P_B,H_NES_CPP::StandardControllerStatus::Pad1P_B);
  Buttons_Init(3,ui->pushButton_1P_AutoB,H_NES_CPP::StandardControllerStatus::Pad1P_AutoB);
  Buttons_Init(4,ui->pushButton_1P_Select,H_NES_CPP::StandardControllerStatus::Pad1P_Select);
  Buttons_Init(5,ui->pushButton_1P_Start,H_NES_CPP::StandardControllerStatus::Pad1P_Start);
  Buttons_Init(6,ui->pushButton_1P_Up,H_NES_CPP::StandardControllerStatus::Pad1P_Up);
  Buttons_Init(7,ui->pushButton_1P_Down,H_NES_CPP::StandardControllerStatus::Pad1P_Down);
  Buttons_Init(8,ui->pushButton_1P_Left,H_NES_CPP::StandardControllerStatus::Pad1P_Left);
  Buttons_Init(9,ui->pushButton_1P_Right,H_NES_CPP::StandardControllerStatus::Pad1P_Right);
  
  Buttons_Init(10,ui->pushButton_2P_A,H_NES_CPP::StandardControllerStatus::Pad2P_A);
  Buttons_Init(11,ui->pushButton_2P_AutoA,H_NES_CPP::StandardControllerStatus::Pad2P_AutoA);
  Buttons_Init(12,ui->pushButton_2P_B,H_NES_CPP::StandardControllerStatus::Pad2P_B);
  Buttons_Init(13,ui->pushButton_2P_AutoB,H_NES_CPP::StandardControllerStatus::Pad2P_AutoB);
  Buttons_Init(14,ui->pushButton_2P_Select,H_NES_CPP::StandardControllerStatus::Pad2P_Select);
  Buttons_Init(15,ui->pushButton_2P_Start,H_NES_CPP::StandardControllerStatus::Pad2P_Start);
  Buttons_Init(16,ui->pushButton_2P_Up,H_NES_CPP::StandardControllerStatus::Pad2P_Up);
  Buttons_Init(17,ui->pushButton_2P_Down,H_NES_CPP::StandardControllerStatus::Pad2P_Down);
  Buttons_Init(18,ui->pushButton_2P_Left,H_NES_CPP::StandardControllerStatus::Pad2P_Left);
  Buttons_Init(19,ui->pushButton_2P_Right,H_NES_CPP::StandardControllerStatus::Pad2P_Right);

  mapper=new QSignalMapper(parent);
  connect(mapper,static_cast<void (QSignalMapper::*)(int)>(&QSignalMapper::mappedInt),this,&InputSettingsDialog::Buttons_Event);

  for(int id=0;id<20;id++){
    mapper->setMapping(Buttons[id].btn,(int)id);
    connect(Buttons[id].btn,static_cast<void (QPushButton::*)(bool)>(&QPushButton::clicked),mapper,static_cast<void (QSignalMapper::*)()>(&QSignalMapper::map));
  }

  HighLightBtnId=-1;


  IsInit=true;
}

InputSettingsDialog::~InputSettingsDialog(){
  delete ui;
}

void InputSettingsDialog::PadPressEvent(int Index,uint32_t Button){
  int r;

  if(HighLightBtnId<0){
    return;
  }

  r=((MainWindow*)mainWindow)->nes_InputMapper->SetKeyMapper(Buttons[HighLightBtnId].KeyVal,Index,Button);
  if(r!=0){
    qDebug()<<"SetKeyMapper()错误:"<<r;
  }

  Buttons[HighLightBtnId].btn->setText(MainWindow::ButtonToString((MainWindow::InputDeviceIndex)Index,Button));

  HighLightBtnId=-1;
}

void InputSettingsDialog::PadReleaseEvent(int Index,uint32_t Button){

  Q_UNUSED(Index);
  Q_UNUSED(Button);

  // if(HighLightBtnId<0){
  //   return;
  // }



  // HighLightBtnId=-1;
}




void InputSettingsDialog::closeEvent(QCloseEvent* event){

  Q_UNUSED(event);

  ((MainWindow*)mainWindow)->InputSettingsDialogCloseEvent();
}

void InputSettingsDialog::keyPressEvent(QKeyEvent* event){

  ((MainWindow*)mainWindow)->NES_InputPress(MainWindow::InputDeviceIndex::Keyboard,(uint32_t)event->key());
}

void InputSettingsDialog::keyReleaseEvent(QKeyEvent* event){

  ((MainWindow*)mainWindow)->NES_InputRelease(MainWindow::InputDeviceIndex::Keyboard,(uint32_t)event->key());
}




void InputSettingsDialog::Buttons_Init(int instanceIndex,QPushButton* btn,uint32_t KeyVal){
  int Index;
  uint32_t Button;
  int r;

  Buttons[instanceIndex].btn=btn;
  Buttons[instanceIndex].KeyVal=KeyVal;
  r=((MainWindow*)mainWindow)->nes_InputMapper->GetKeyMapper(KeyVal,&Index,&Button);

  if(r!=0){
    qDebug()<<"GetKeyMapper()错误:"<<r;
  }

  btn->setText(MainWindow::ButtonToString((MainWindow::InputDeviceIndex)Index,Button));
}

void InputSettingsDialog::Buttons_Event(int id){
  int r;

  if(HighLightBtnId<0){

    Buttons[id].btn->setText("请按下一个按键");

    HighLightBtnId=id;
  }else if(HighLightBtnId==id){

    Buttons[id].btn->setText("Unknow");

    r=((MainWindow*)mainWindow)->nes_InputMapper->SetKeyMapper(Buttons[HighLightBtnId].KeyVal,-1,0U);
    if(r!=0){
      qDebug()<<"SetKeyMapper()错误:"<<r;
    }

    HighLightBtnId=-1;
  }
}



